//
//  SinaTabBarView.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit

class SinaTabBarView: UITabBar {

    var composeBtnDidClick: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeBtn)
        
        composeBtn.addTarget(self, action:#selector(composeClick), for: .touchUpInside)
        
        
    }
    
    // MARK:
    // MARK: - 点击撰写按钮触发监听
    @objc private func composeClick(){
        
        composeBtnDidClick?()
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK:
    // MARK: - 布局tabBar
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = UIScreen.main.bounds.width * 0.2
        let h = bounds.height
        
        var index = 0
        for itemView in subviews{
            
            if itemView.isKind(of: NSClassFromString("UITabBarButton")!){
                
                itemView.frame = CGRect(x: w * CGFloat(index), y: 0, width: w, height: h)
             
                index += (index == 1 ? 2 : 1)
            }
            
        }
        
        composeBtn.bounds.size = CGSize(width: w, height: h)
        composeBtn.center = CGPoint(x: center.x, y: h * 0.5)
        
    }
    
    
    
    // MARK:
    // MARK: - 添加中间的按钮
    lazy var composeBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
//        btn.sizeToFit()
        
        return btn
    }()
    
    
    
    
    
    
    

}
